package com.ruoyi.server.api.booking.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.server.api.booking.domain.ImgBooking;
import com.ruoyi.server.api.booking.service.IImgBookingService;

import javax.servlet.http.HttpServletResponse;

/**
 * 预约到店Controller
 *
 * @author lx
 * @date 2024-10-22
 */
@RestController
@RequestMapping("/booking/booking")
public class ImgBookingController extends BaseController {
    @Autowired
    private IImgBookingService imgBookingService;

    /**
     * 查询预约到店列表
     */
    @PreAuthorize("@ss.hasPermi('booking:booking:list')")
    @GetMapping("/list")
    public TableDataInfo list(ImgBooking imgBooking) {
        startPage();
        List<ImgBooking> list = imgBookingService.selectImgBookingList(imgBooking);
        return getDataTable(list);
    }

    /**
     * 导出预约到店列表
     */
    @PreAuthorize("@ss.hasPermi('booking:booking:export')")
    @Log(title = "预约到店", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImgBooking imgBooking) {
        List<ImgBooking> list = imgBookingService.selectImgBookingList(imgBooking);
        ExcelUtil<ImgBooking> util = new ExcelUtil<ImgBooking>(ImgBooking.class);
        util.exportExcel(response, list, "预约到店数据");
    }

    /**
     * 获取预约到店详细信息
     */
    @PreAuthorize("@ss.hasPermi('booking:booking:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(imgBookingService.selectImgBookingById(id));
    }

    /**
     * 新增预约到店
     */
    @PreAuthorize("@ss.hasPermi('booking:booking:add')")
    @Log(title = "预约到店", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImgBooking imgBooking) {
        return toAjax(imgBookingService.insertImgBooking(imgBooking));
    }

    /**
     * 修改预约到店
     */
    @PreAuthorize("@ss.hasPermi('booking:booking:edit')")
    @Log(title = "预约到店", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImgBooking imgBooking) {
        return toAjax(imgBookingService.updateImgBooking(imgBooking));
    }

    /**
     * 删除预约到店
     */
    @PreAuthorize("@ss.hasPermi('booking:booking:remove')")
    @Log(title = "预约到店", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(imgBookingService.deleteImgBookingByIds(ids));
    }

    /**
     * @param imgBooking
     * @Author Lx
     * @Description 新增预约的方法
     * @Date 2024/10/22 15:34
     * @Return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("saveBooking")
    public AjaxResult saveBooking(@RequestBody ImgBooking imgBooking) {
        imgBooking.setCreateTime(new Date());
        // 默认为正常状态
//        imgBooking.setStatus(0L);
        return toAjax(imgBookingService.saveBooking(imgBooking));
    }

    /**
     * @param userId
     * @Author Lx
     * @Description 根据用户id查找预约信息列表
     * @Date 2024/10/22 15:36
     * @Return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("bookingListByuserId")
    public AjaxResult bookingListByuserId(String userId) {
        return success(imgBookingService.selectImgBookingListByuserId(userId));
    }

    /**
     * @Author Lx
     * @Description 查找预约信息列表
     * @Date 2024/10/23 15:14
     * @Return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("selectBookings")
    public AjaxResult selectBookings() {
        return success(imgBookingService.selectBoikings());
    }

    @GetMapping("selectBookingsByUser")
    public AjaxResult selectBookingsByUser(String userId) {
        return success(imgBookingService.selectBookingsByUser(userId));
}

    /**
     * @Author Lx
     * @Description 根据id删除预约信息
     * @Date 2024/11/29 10:11
     * @Return int
     * @param id
     */
    @DeleteMapping("deleteBookingById")
    public AjaxResult deleteBookingById(@RequestParam Long id) {
        return toAjax(imgBookingService.deleteBookingById(id));
    }

    /**
     * @param id
     * @Date 2024/10/23 15:14
     * @Return com.ruoyi.common.core.domain.AjaxResult
     */
    @PutMapping("updateBookingStatus")
    public AjaxResult updateBookingStatus(@RequestParam Long id) {
        return toAjax(imgBookingService.updateBookingStatus(id));
    }

    /**
    * @Author Lx
    * @Description 完成订单的方法
    * @Date 2024/12/2 9:05
    * @Return com.ruoyi.common.core.domain.AjaxResult
    * @param id
    */
    @PutMapping("completeOrder")
    public AjaxResult completeOrder(@RequestParam Long id) {
        return toAjax(imgBookingService.completeOrder(id));
    }
}
